package lg.iu;

import java.io.File;
import java.io.IOException;

import lg.iu.transforms.AnimableListTransform;
import lg.iu.transforms.ClientScriptTransform;
import lg.iu.transforms.ClientTransform;
import lg.iu.transforms.InterfaceCacheFinder;
import lg.iu.transforms.RenderDataTransform;
import lg.iu.transforms.input.CanvasTransform;
import lg.iu.transforms.input.KeyboardTransform;
import lg.iu.transforms.input.MouseTransform;
import lg.iu.transforms.tiledata.TiledataGetterTransform;
import lg.iu.transforms.tiledata.TiledataTransform;
import wolf.iu.framework.ClassesHandler;
import wolf.iu.framework.Timer;
import wolf.iu.framework.Updater;

public class Main {

    public static void main(final String[] args) {
        try {
            new Main().start(args);
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }
    
    ClassesHandler classes;
    Updater<ClassHooks, FieldHooks> updater;
    
    private static final File rsjar = new File("rsjar.jar");
    
    public void start(final String[] args) throws IOException {
        
        if (!rsjar.exists() || (args.length > 0 && args[0].equals("-fetchnewest"))) {
            System.out.println("Fetching newest rs.jar");
            final Timer fetchTimer = new Timer();
            ReadURLVersion.fetchNewest(rsjar);
            System.out.println("Fetched in " + fetchTimer.stop() + "ms");
        }
        
        System.out.println("Loading classes");
        this.classes = new ClassesHandler(rsjar);
        System.out.println("Classes loaded");
        
        final VersionVisitor vv = new VersionVisitor();
        this.classes.getClass("client").accept(vv);
        System.out.println("Hooking for #" + vv.getVersion());
        
        updater = new Updater<ClassHooks, FieldHooks>(classes, "lg/iu/testpackage/", ClassHooks.values(), FieldHooks.values());
        
        updater.addDeob(new StringDecryption());
        
        updater.addTransform(new ClientTransform());
        updater.addTransform(new ClientScriptTransform());
        updater.addTransform(new InterfaceCacheFinder());
        updater.addTransform(new TiledataTransform());
        updater.addTransform(new TiledataGetterTransform());
        
        updater.addTransform(new MouseTransform());
        updater.addTransform(new KeyboardTransform());
        updater.addTransform(new CanvasTransform());
        
        updater.addTransform(new AnimableListTransform());
        updater.addTransform(new RenderDataTransform());
        
        updater.run();
        updater.printLog();
    }

}
